#include "WorldSession.h"
#include "SocketServer.h"
WorldSession::WorldSession(uint32 _socket)
{
	std::thread(&WorldSession::AutoSendMessageThread, this).detach();
	CanSendNextPacket = true;
}

WorldSession::~WorldSession()
{

}

void WorldSession::AddMessageToQueue(WorldPacket _packet)		
{
	while (1)
	{
		{
			ThreadLocker Locker(SessionMessageLock);
			sMessageQueue.push_back(_packet);
			return;
		}
		Sleep(1);
	}
}

void WorldSession::AutoSendMessageThread()
{
	while (1)
	{

		if (sMessageQueue.size() && CanSendNextPacket)
		{
			ThreadLocker Locker(SessionMessageLock);
			sSocketServer->sendMessage(GetSocket(), sMessageQueue.at(0), sizeof(sMessageQueue.at(0)));
			std::vector<WorldPacket>::iterator itr = sMessageQueue.begin();
			sMessageQueue.erase(itr);
			CanSendNextPacket = false;
		}
		Sleep(10);
	}
}